<?php

namespace app\common\model\wechat;

use app\common\model\BaseModel;
use hiduoke\services\facade\WechatService;

class WechatUserWallet extends BaseModel
{
    /**
     * @var mixed
     */
    private $plaid;

    public function __construct(array $data = [])
    {
        parent::__construct($data);

        $this->plaid = get_plaid();
    }

    /**
     * 创建用户钱包
     * @param $user_id
     * @param $plaid
     * @return array|\think\Model
     */
    public static function createUserWallet($user_id)
    {
        return (new static())->getUserWallet($user_id);
    }

    /**
     * 获取用户钱包
     * @param $user_id
     * @param $plaid
     * @return array|\think\Model
     */
    public function getUserWallet($user_id = 0)
    {
        $user_id = $user_id ?: WechatUser::getUser()['id'];
        $wallet = $this->where(['uid' => $user_id, 'plaid' => $this->plaid])->findOrEmpty();
        if ($wallet->isEmpty()) {
            $data = array(
                'uid'   => $user_id,
                'plaid' => $this->plaid
            );
            $wallet->save($data);
        }
        return $wallet;
    }

    /**
     * 冻结金额转入余额
     * @param float $move_money
     * @param string $desc
     * @param int $type
     * @param int $user_id
     * @return array|\think\Model
     */
    public function frozenToMoney($move_money, $desc = '金额解冻', $type = 1, $user_id = 0)
    {
        return $this->change($move_money, -$move_money, $desc, $type, $user_id);
    }

    /**
     * 改变用户冻结金额
     * @param $frozen_money
     * @param string $desc
     * @param int $type
     * @param int $user_id
     * @return array|\think\Model
     */
    public function changeFrozenMoney($frozen_money, $desc = '', $type = 0, $user_id = 0)
    {
        return $this->change(0, $frozen_money, $desc, $type, $user_id);

    }

    /**
     * 改变用户金额
     * @param $user_id
     * @param $money
     * @param string $desc
     * @param int $type
     * @return array|\think\Model
     */
    public function changeMoney($money, $desc = '', $type = 0, $user_id = 0)
    {
        return $this->change($money, 0, $desc, $type, $user_id);
    }

    /**
     * 改变用户所有金额
     * @param $user_id
     * @param $money
     * @param $frozen_money
     * @param string $desc
     * @param int $type
     * @return array|\think\Model
     */
    public function change($money, $frozen_money, $desc = '', $type = 0, $user_id = 0)
    {
        $user_id = $user_id ?: WechatUser::getUser()['id'];
        $wallet = $this->getUserWallet($user_id);
        $log = new WechatUserWalletLog();
        $log_data = array(
            'uid'                 => $user_id,
            'money'               => $money,
            'frozen_money'        => $frozen_money,
            'change_desc'         => $desc,
            'change_type'         => $type,
            'before_money'        => $wallet->money,
            'after_money'         => $wallet->money + $money,
            'before_frozen_money' => $wallet->frozen_money,
            'after_frozen_money'  => $wallet->frozen_money + $frozen_money,
            'plaid'               => $this->plaid
        );
        $wallet_data = array(
            'money'        => $log_data['after_money'],
            'frozen_money' => $log_data['after_frozen_money'],
        );
        $wallet->save($wallet_data);
        $log->save($log_data);
        return $wallet;
    }


    /**
     * 获取器
     * @return \think\model\relation\HasOne
     */
    public function user()
    {
        return $this->hasOne(WechatUser::class, 'id', 'uid');
    }

    /**
     * 获取金额记录
     * @return \think\model\relation\HasMany
     */
    public function userWalletLog()
    {
        return $this->hasMany(WechatUserWalletLog::class, 'uid', 'uid')->order('id DESC')->limit(10);
    }

    public function setMoneyAttr($value)
    {
        return sprintf('%.2f', $value);
    }


    public function setFrozenMoneyAttr($value)
    {
        return sprintf('%.2f', $value);
    }
}
